package l1j.opqlo.掛機;

import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

import l1j.opqlo.Server.L1World.L1World;
import l1j.server.Thread.GeneralThreadPool;
import l1j.server.server.model.Instance.L1MonsterInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.skill.L1SkillUse;

public class RobotThread implements Runnable {
	public static CopyOnWriteArrayList<L1PcInstance>LIST = new CopyOnWriteArrayList<L1PcInstance>();

	public void start(){
		GeneralThreadPool.getInstance().execute(this);
	}

	@Override
	public void run() {
		while(true){
			if(LIST.isEmpty()){
				continue;
			}
			Iterator<L1PcInstance> ite =LIST.iterator();

			while(ite.hasNext()){
				L1PcInstance pc  =ite.next();
				if(pc.getNetConnection() != null){
					RobotTemp temp = pc.getRobotTemp();
					if (!pc.isSkillDelay() && temp.checkDelay()) {
						final L1MonsterInstance mob = L1World.getInstance().getVisibleMob(pc , 5);
						if(mob != null && mob.isDead() == false){
							new L1SkillUse().handleCommands(pc, temp.getSkillId(), mob.getId(), mob.getX(), mob.getY(),null, 0, L1SkillUse.TYPE_NORMAL);
							temp.setDelay();
						}
					}
				}else{
					LIST.remove(pc);
				}
			}
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}
